using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections.Generic;
using System.Linq;

namespace StarSkipGame
{
    /// <summary>
    /// 飞船皮肤类型枚举
    /// </summary>
    public enum ShipSkinType
    {
        Classic,        // 经典飞船
        Fighter,        // 战斗机
        Explorer,       // 探索者
        Stealth,        // 隐形飞船
        BattleCruiser,  // 战列巡洋舰
        Interceptor,    // 拦截机
        Scout,          // 侦察机
        Destroyer       // 驱逐舰
    }

    /// <summary>
    /// 飞船皮肤类
    /// </summary>
    public class ShipSkin
    {
        public ShipSkinType Type { get; private set; }
        public string Name { get; private set; }
        public string Description { get; private set; }
        public Color PrimaryColor { get; private set; }
        public Color SecondaryColor { get; private set; }
        public Color AccentColor { get; private set; }
        public bool IsUnlocked { get; set; }
        public bool IsDefault { get; private set; }

        public ShipSkin(ShipSkinType type, string name, string description, 
            Color primaryColor, Color secondaryColor, Color accentColor, bool isDefault = false)
        {
            Type = type;
            Name = name;
            Description = description;
            PrimaryColor = primaryColor;
            SecondaryColor = secondaryColor;
            AccentColor = accentColor;
            IsUnlocked = isDefault;
            IsDefault = isDefault;
        }

        /// <summary>
        /// 绘制飞船
        /// </summary>
        /// <param name="g">绘图对象</param>
        /// <param name="x">X坐标</param>
        /// <param name="y">Y坐标</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <param name="rotation">旋转角度</param>
        public void Draw(Graphics g, float x, float y, float width, float height, float rotation = 0)
        {
            // 保存当前图形状态
            Matrix originalMatrix = g.Transform;

            // 设置旋转中心点
            float centerX = x + width / 2;
            float centerY = y + height / 2;

            // 应用旋转
            if (rotation != 0)
            {
                g.TranslateTransform(centerX, centerY);
                g.RotateTransform(rotation);
                g.TranslateTransform(-centerX, -centerY);
            }

            // 根据皮肤类型绘制不同的飞船
            switch (Type)
            {
                case ShipSkinType.Classic:
                    DrawClassicShip(g, x, y, width, height);
                    break;
                case ShipSkinType.Fighter:
                    DrawFighterShip(g, x, y, width, height);
                    break;
                case ShipSkinType.Explorer:
                    DrawExplorerShip(g, x, y, width, height);
                    break;
                case ShipSkinType.Stealth:
                    DrawStealthShip(g, x, y, width, height);
                    break;
                case ShipSkinType.BattleCruiser:
                    DrawBattleCruiserShip(g, x, y, width, height);
                    break;
                case ShipSkinType.Interceptor:
                    DrawInterceptorShip(g, x, y, width, height);
                    break;
                case ShipSkinType.Scout:
                    DrawScoutShip(g, x, y, width, height);
                    break;
                case ShipSkinType.Destroyer:
                    DrawDestroyerShip(g, x, y, width, height);
                    break;
            }

            // 恢复图形状态
            g.Transform = originalMatrix;
        }

        /// <summary>
        /// 绘制经典飞船
        /// </summary>
        private void DrawClassicShip(Graphics g, float x, float y, float width, float height)
        {
            using (SolidBrush primaryBrush = new SolidBrush(PrimaryColor))
            using (SolidBrush secondaryBrush = new SolidBrush(SecondaryColor))
            using (SolidBrush accentBrush = new SolidBrush(AccentColor))
            {
                // 主体
                PointF[] body = {
                    new PointF(x + width * 0.5f, y),
                    new PointF(x + width * 0.8f, y + height * 0.6f),
                    new PointF(x + width * 0.7f, y + height),
                    new PointF(x + width * 0.3f, y + height),
                    new PointF(x + width * 0.2f, y + height * 0.6f)
                };
                g.FillPolygon(primaryBrush, body);

                // 驾驶舱
                g.FillEllipse(secondaryBrush, x + width * 0.35f, y + height * 0.2f, width * 0.3f, height * 0.3f);

                // 引擎
                g.FillEllipse(accentBrush, x + width * 0.25f, y + height * 0.7f, width * 0.2f, height * 0.2f);
                g.FillEllipse(accentBrush, x + width * 0.55f, y + height * 0.7f, width * 0.2f, height * 0.2f);
            }
        }

        /// <summary>
        /// 绘制战斗机
        /// </summary>
        private void DrawFighterShip(Graphics g, float x, float y, float width, float height)
        {
            using (SolidBrush primaryBrush = new SolidBrush(PrimaryColor))
            using (SolidBrush secondaryBrush = new SolidBrush(SecondaryColor))
            using (SolidBrush accentBrush = new SolidBrush(AccentColor))
            {
                // 主体
                PointF[] body = {
                    new PointF(x + width * 0.5f, y),
                    new PointF(x + width * 0.9f, y + height * 0.4f),
                    new PointF(x + width * 0.8f, y + height * 0.8f),
                    new PointF(x + width * 0.6f, y + height),
                    new PointF(x + width * 0.4f, y + height),
                    new PointF(x + width * 0.2f, y + height * 0.8f),
                    new PointF(x + width * 0.1f, y + height * 0.4f)
                };
                g.FillPolygon(primaryBrush, body);

                // 机翼
                PointF[] wings = {
                    new PointF(x + width * 0.3f, y + height * 0.3f),
                    new PointF(x, y + height * 0.5f),
                    new PointF(x + width * 0.2f, y + height * 0.7f),
                    new PointF(x + width * 0.4f, y + height * 0.6f)
                };
                g.FillPolygon(secondaryBrush, wings);

                PointF[] wings2 = {
                    new PointF(x + width * 0.7f, y + height * 0.3f),
                    new PointF(x + width, y + height * 0.5f),
                    new PointF(x + width * 0.8f, y + height * 0.7f),
                    new PointF(x + width * 0.6f, y + height * 0.6f)
                };
                g.FillPolygon(secondaryBrush, wings2);

                // 驾驶舱
                g.FillEllipse(accentBrush, x + width * 0.4f, y + height * 0.15f, width * 0.2f, height * 0.25f);
            }
        }

        /// <summary>
        /// 绘制探索者飞船
        /// </summary>
        private void DrawExplorerShip(Graphics g, float x, float y, float width, float height)
        {
            using (SolidBrush primaryBrush = new SolidBrush(PrimaryColor))
            using (SolidBrush secondaryBrush = new SolidBrush(SecondaryColor))
            using (SolidBrush accentBrush = new SolidBrush(AccentColor))
            {
                // 主体（圆形）
                g.FillEllipse(primaryBrush, x + width * 0.1f, y + height * 0.1f, width * 0.8f, height * 0.8f);

                // 天线
                g.FillRectangle(secondaryBrush, x + width * 0.45f, y, width * 0.1f, height * 0.2f);
                g.FillEllipse(accentBrush, x + width * 0.4f, y, width * 0.2f, height * 0.1f);

                // 观察窗
                g.FillEllipse(secondaryBrush, x + width * 0.25f, y + height * 0.3f, width * 0.5f, height * 0.4f);
                g.FillEllipse(accentBrush, x + width * 0.3f, y + height * 0.35f, width * 0.4f, height * 0.3f);
            }
        }

        /// <summary>
        /// 绘制隐形飞船
        /// </summary>
        private void DrawStealthShip(Graphics g, float x, float y, float width, float height)
        {
            using (SolidBrush primaryBrush = new SolidBrush(PrimaryColor))
            using (SolidBrush secondaryBrush = new SolidBrush(SecondaryColor))
            using (SolidBrush accentBrush = new SolidBrush(AccentColor))
            {
                // 主体（三角形）
                PointF[] body = {
                    new PointF(x + width * 0.5f, y),
                    new PointF(x + width * 0.2f, y + height),
                    new PointF(x + width * 0.8f, y + height)
                };
                g.FillPolygon(primaryBrush, body);

                // 隐形涂层
                PointF[] coating = {
                    new PointF(x + width * 0.3f, y + height * 0.2f),
                    new PointF(x + width * 0.7f, y + height * 0.2f),
                    new PointF(x + width * 0.6f, y + height * 0.8f),
                    new PointF(x + width * 0.4f, y + height * 0.8f)
                };
                g.FillPolygon(secondaryBrush, coating);

                // 传感器
                g.FillEllipse(accentBrush, x + width * 0.45f, y + height * 0.4f, width * 0.1f, height * 0.2f);
            }
        }

        /// <summary>
        /// 绘制战列巡洋舰
        /// </summary>
        private void DrawBattleCruiserShip(Graphics g, float x, float y, float width, float height)
        {
            using (SolidBrush primaryBrush = new SolidBrush(PrimaryColor))
            using (SolidBrush secondaryBrush = new SolidBrush(SecondaryColor))
            using (SolidBrush accentBrush = new SolidBrush(AccentColor))
            {
                // 主体
                g.FillRectangle(primaryBrush, x + width * 0.2f, y + height * 0.1f, width * 0.6f, height * 0.8f);

                // 炮塔
                g.FillEllipse(secondaryBrush, x + width * 0.1f, y + height * 0.3f, width * 0.2f, height * 0.4f);
                g.FillEllipse(secondaryBrush, x + width * 0.7f, y + height * 0.3f, width * 0.2f, height * 0.4f);

                // 主炮
                g.FillRectangle(accentBrush, x + width * 0.45f, y, width * 0.1f, height * 0.3f);
                g.FillRectangle(accentBrush, x + width * 0.45f, y + height * 0.7f, width * 0.1f, height * 0.3f);
            }
        }

        /// <summary>
        /// 绘制拦截机
        /// </summary>
        private void DrawInterceptorShip(Graphics g, float x, float y, float width, float height)
        {
            using (SolidBrush primaryBrush = new SolidBrush(PrimaryColor))
            using (SolidBrush secondaryBrush = new SolidBrush(SecondaryColor))
            using (SolidBrush accentBrush = new SolidBrush(AccentColor))
            {
                // 主体
                PointF[] body = {
                    new PointF(x + width * 0.5f, y),
                    new PointF(x + width * 0.8f, y + height * 0.3f),
                    new PointF(x + width * 0.9f, y + height * 0.7f),
                    new PointF(x + width * 0.7f, y + height),
                    new PointF(x + width * 0.3f, y + height),
                    new PointF(x + width * 0.1f, y + height * 0.7f),
                    new PointF(x + width * 0.2f, y + height * 0.3f)
                };
                g.FillPolygon(primaryBrush, body);

                // 推进器
                g.FillEllipse(secondaryBrush, x + width * 0.35f, y + height * 0.8f, width * 0.15f, height * 0.15f);
                g.FillEllipse(secondaryBrush, x + width * 0.5f, y + height * 0.8f, width * 0.15f, height * 0.15f);

                // 驾驶舱
                g.FillEllipse(accentBrush, x + width * 0.4f, y + height * 0.2f, width * 0.2f, height * 0.2f);
            }
        }

        /// <summary>
        /// 绘制侦察机
        /// </summary>
        private void DrawScoutShip(Graphics g, float x, float y, float width, float height)
        {
            using (SolidBrush primaryBrush = new SolidBrush(PrimaryColor))
            using (SolidBrush secondaryBrush = new SolidBrush(SecondaryColor))
            using (SolidBrush accentBrush = new SolidBrush(AccentColor))
            {
                // 主体（小圆形）
                g.FillEllipse(primaryBrush, x + width * 0.2f, y + height * 0.2f, width * 0.6f, height * 0.6f);

                // 传感器阵列
                g.FillEllipse(secondaryBrush, x + width * 0.1f, y + height * 0.1f, width * 0.8f, height * 0.8f);
                g.FillEllipse(primaryBrush, x + width * 0.15f, y + height * 0.15f, width * 0.7f, height * 0.7f);

                // 天线
                g.FillRectangle(accentBrush, x + width * 0.48f, y, width * 0.04f, height * 0.15f);
                g.FillRectangle(accentBrush, x + width * 0.48f, y + height * 0.85f, width * 0.04f, height * 0.15f);
            }
        }

        /// <summary>
        /// 绘制驱逐舰
        /// </summary>
        private void DrawDestroyerShip(Graphics g, float x, float y, float width, float height)
        {
            using (SolidBrush primaryBrush = new SolidBrush(PrimaryColor))
            using (SolidBrush secondaryBrush = new SolidBrush(SecondaryColor))
            using (SolidBrush accentBrush = new SolidBrush(AccentColor))
            {
                // 主体
                g.FillRectangle(primaryBrush, x + width * 0.1f, y + height * 0.2f, width * 0.8f, height * 0.6f);

                // 装甲板
                g.FillRectangle(secondaryBrush, x + width * 0.15f, y + height * 0.25f, width * 0.7f, height * 0.5f);

                // 武器系统
                g.FillRectangle(accentBrush, x + width * 0.05f, y + height * 0.4f, width * 0.1f, height * 0.2f);
                g.FillRectangle(accentBrush, x + width * 0.85f, y + height * 0.4f, width * 0.1f, height * 0.2f);

                // 指挥塔
                g.FillRectangle(secondaryBrush, x + width * 0.4f, y + height * 0.1f, width * 0.2f, height * 0.3f);
            }
        }
    }

    /// <summary>
    /// 飞船皮肤管理器
    /// </summary>
    public class ShipSkinManager
    {
        private static ShipSkinManager instance;
        private static readonly object lockObject = new object();
        private Dictionary<ShipSkinType, ShipSkin> skins;
        private ShipSkinType currentSkin;

        public static ShipSkinManager Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (lockObject)
                    {
                        if (instance == null)
                        {
                            instance = new ShipSkinManager();
                        }
                    }
                }
                return instance;
            }
        }

        private ShipSkinManager()
        {
            InitializeSkins();
            currentSkin = ShipSkinType.Classic;
        }

        /// <summary>
        /// 初始化所有皮肤
        /// </summary>
        private void InitializeSkins()
        {
            skins = new Dictionary<ShipSkinType, ShipSkin>
            {
                { ShipSkinType.Classic, new ShipSkin(ShipSkinType.Classic, "经典飞船", "传统的太空飞船设计", 
                    Color.LightBlue, Color.White, Color.Yellow, true) },
                
                { ShipSkinType.Fighter, new ShipSkin(ShipSkinType.Fighter, "战斗机", "高速战斗飞船", 
                    Color.Red, Color.DarkRed, Color.Orange) },
                
                { ShipSkinType.Explorer, new ShipSkin(ShipSkinType.Explorer, "探索者", "科学探索飞船", 
                    Color.Green, Color.LightGreen, Color.Cyan) },
                
                { ShipSkinType.Stealth, new ShipSkin(ShipSkinType.Stealth, "隐形飞船", "隐形技术飞船", 
                    Color.DarkGray, Color.Gray, Color.Black) },
                
                { ShipSkinType.BattleCruiser, new ShipSkin(ShipSkinType.BattleCruiser, "战列巡洋舰", "重型战斗飞船", 
                    Color.Navy, Color.Blue, Color.Silver) },
                
                { ShipSkinType.Interceptor, new ShipSkin(ShipSkinType.Interceptor, "拦截机", "快速拦截飞船", 
                    Color.Purple, Color.Magenta, Color.Pink) },
                
                { ShipSkinType.Scout, new ShipSkin(ShipSkinType.Scout, "侦察机", "轻型侦察飞船", 
                    Color.Lime, Color.Yellow, Color.White) },
                
                { ShipSkinType.Destroyer, new ShipSkin(ShipSkinType.Destroyer, "驱逐舰", "多功能战斗飞船", 
                    Color.Brown, Color.DarkOrange, Color.Gold) }
            };
        }

        /// <summary>
        /// 获取当前皮肤
        /// </summary>
        public ShipSkin GetCurrentSkin()
        {
            return skins[currentSkin];
        }

        /// <summary>
        /// 设置当前皮肤
        /// </summary>
        public void SetCurrentSkin(ShipSkinType skinType)
        {
            if (skins.ContainsKey(skinType) && skins[skinType].IsUnlocked)
            {
                currentSkin = skinType;
            }
        }

        /// <summary>
        /// 解锁皮肤
        /// </summary>
        public void UnlockSkin(ShipSkinType skinType)
        {
            if (skins.ContainsKey(skinType))
            {
                skins[skinType].IsUnlocked = true;
            }
        }

        /// <summary>
        /// 获取所有皮肤
        /// </summary>
        public Dictionary<ShipSkinType, ShipSkin> GetAllSkins()
        {
            return new Dictionary<ShipSkinType, ShipSkin>(skins);
        }

        /// <summary>
        /// 获取已解锁的皮肤
        /// </summary>
        public List<ShipSkin> GetUnlockedSkins()
        {
            return skins.Values.Where(skin => skin.IsUnlocked).ToList();
        }
    }
} 